package com.ihr360.cnb.module.payroll.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.ihr360.cnb.module.payroll.service.PayrollAggregateService;
import com.ihr360.commons.entity.AuthUserInfo;
import com.ihr360.commons.exception.Ihr360Exception;
import com.ihr360.commons.lang.CollectionUtils;
import com.ihr360.commons.lang.StringUtils;
import com.ihr360.commons.vo.PageData;
import com.ihr360.commons.vo.ResultInfo;
import com.ihr360.maven.plugin.ModuleCodeStore;
import com.ihr360.maven.plugin.annotation.FunctionDefinition;
import com.ihr360.maven.plugin.annotation.ModuleDefinition;
import com.ihr360.payroll.entity.staff.StaffPayrollInfoJournalEntity;
import com.ihr360.payroll.entity.staff.StaffSalaryEntity;
import com.ihr360.payroll.request.StaffPayrollInfoJournalRequest;
import com.ihr360.payroll.request.StaffPayrollInfoRequest;
import com.ihr360.payroll.request.StaffPayrollInfosRequest;
import com.ihr360.payroll.request.StaffSalaryProfileSearchBody;
import com.ihr360.payroll.response.StaffPayrollInfoResponse;
import com.ihr360.shared.advice.response.DisableResponseAdvice;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author Eric
 */
@RestController
@RequestMapping("/payrollInfo")
@DisableResponseAdvice
@ModuleDefinition(mainModuleCode = ModuleCodeStore.Module.SALARY_CODE,
        mainModuleName = ModuleCodeStore.Module.SALARY_NAME,
        subModuleCode = ModuleCodeStore.Module.Salary.STAFF_PAY_ROLLINFO_CODE,
        subModuleName = ModuleCodeStore.Module.Salary.STAFF_PAY_ROLLINFO_NAME,
        hasExtendsAbstractRestController = false)
public class StaffSalaryAggregateController {


    @Autowired
    private PayrollAggregateService payrollAggregateService;

    /**
     * 员工薪资信息保存
     */
    @FunctionDefinition(functionName = "员工薪资信息保存", functionCode = ModuleCodeStore.Function.EDIT)
    @ApiOperation(value = "/staffSalaryInfo/save", httpMethod = "POST", notes = "员工薪资信息保存")
    @RequestMapping(value = "/staffSalaryInfo/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public void saveStaffSalaryInfo(AuthUserInfo userInfo, @RequestBody StaffSalaryEntity staffSalaryEntity) {
        if (staffSalaryEntity == null) {
            throw new Ihr360Exception(null, "薪资信息不可为空");
        }
        staffSalaryEntity.setCompanyId(userInfo.getCompanyId());
        payrollAggregateService.saveStaffSalaryInfo(Lists.newArrayList(staffSalaryEntity.getStaffId()),staffSalaryEntity);
    }


    @ApiOperation(value = "查询有权限的员工的薪资调整记录")
    @PostMapping(value = "/list/payrollInfoJournals", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @FunctionDefinition(functionName = "查询有权限的员工的薪资调整记录", functionCode = ModuleCodeStore.Function.VIEW)
    public PageData<StaffPayrollInfoJournalEntity> findAllAuthJournals(@RequestBody StaffPayrollInfoJournalRequest param) {
        return payrollAggregateService.findAllJournals(param);
    }


    /**
     * 获取有权限的员工
     *
     * @param param
     * @return
     */
    @PostMapping(value = "/list/currentEffective")
    @FunctionDefinition(functionName = "获取当前生效薪资档案", functionCode = ModuleCodeStore.Function.VIEW)
    public StaffPayrollInfoResponse getAuthStaffs(@RequestBody StaffSalaryProfileSearchBody param) {
        return payrollAggregateService.getCurrentEffectiveStaffSalarys(param,false);
    }


    @FunctionDefinition(functionName = "导出当前员工薪资", functionCode = ModuleCodeStore.Function.EXPORT)
    @RequestMapping(value = "/export/currentEffective",  method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public void export(@RequestParam("param") String param, HttpServletResponse response) throws IOException {
        response.setHeader("x-filename", URLEncoder.encode("员工薪资.xls", "UTF-8"));
        StaffSalaryProfileSearchBody paramObj = new StaffSalaryProfileSearchBody();
        if (StringUtils.isNotEmpty(param)) {
            paramObj = JSONObject.parseObject(param, StaffSalaryProfileSearchBody.class);
        }
        payrollAggregateService.exportCurrent(paramObj, response.getOutputStream());
    }


    @ApiOperation(value = "修改员工薪资档案薪资项数据")
    @PostMapping(value = "/salaryProfile/wrapper/update", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @FunctionDefinition(functionName = "修改员工薪资档案薪资项数据", functionCode = ModuleCodeStore.Function.EDIT)
    public ResultInfo updateSalaryfile(@RequestBody StaffPayrollInfosRequest staffpayrollInfos) {
        List<String> staffIdList = Lists.newArrayList();
        List<StaffPayrollInfoRequest> staffPayrollInfoRequestList = staffpayrollInfos.getPayrollInfos();
        if (CollectionUtils.isNotEmpty(staffPayrollInfoRequestList)) {
            for (StaffPayrollInfoRequest datalist : staffPayrollInfoRequestList) {
                staffIdList.add(datalist.getStaffId());
            }
        }
        return payrollAggregateService.adjustSalaryProfileWrapper(staffIdList, staffpayrollInfos);
    }


}
